package com.im.gateway.tcp.push;

import com.im.gateway.tcp.SessionManager;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.socket.SocketChannel;

/**
 * 消息推送管理组件
 * @Created leijun
 * @Date 2021/9/11 10:50 上午
 */
public class PushMessageMananger {

    public void start() {
        new PushMessageThread().start();
    }

    class PushMessageThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(60 * 1000);
                    Long userId = 1L;
                    SessionManager sessionManager = SessionManager.getInstance();
                    SocketChannel socketChannel = sessionManager.getSession(userId);

                    if (null != socketChannel) {
                        byte[] message = "回推消息".getBytes();
                        ByteBuf byteBuf = Unpooled.buffer(message.length);
                        byteBuf.writeBytes(message);
                        socketChannel.writeAndFlush(byteBuf);
                    } else {
                        System.out.println("目标用户已下线，请等待其上线之后再推送");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
